import { invoke } from "@tauri-apps/api";
import { querGameType } from "../interface/Match.ts";
import { goldFormat } from "../Util/util.tsx";

interface ITeam {
  kill: number;
  death: number;
  assists: number;
  goldEarned: number;
  win: boolean;
}
export class champion {
  championId: number;
  championIcon: string;
  spell1Id: number;
  spell1Icon: string;
  spell2Id: number;
  spell2Icon: string;
  perk0: number;
  perk0Icon: string;
  perk1: number;
  perk1Icon: string;
  itemIcons: string[] = Array(7).fill("");
  KDA: string;
  // 小兵
  totalMinionsKilled: number;
  // 经济
  totalGold: string;
  // 伤害
  totalDamageDealtToChampions: string;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  constructor(data: any) {
    this.championId = data.championId;
    this.championIcon = `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champion-icons/${this.championId}.png`;
    this.spell1Id = data.spell1Id;
    this.spell1Icon = getspellImgUrl(data.spell1Id);
    this.spell2Id = data.spell2Id;
    this.spell2Icon = getspellImgUrl(data.spell2Id);
    this.perk0 = data.stats.perk0;
    this.perk0Icon = new URL(
      `/src/assets/runes/${data.stats.perk0}.png`,
      import.meta.url,
    ).href;
    this.perk1 = data.perk1;
    this.perk1Icon = new URL(
      `/src/assets/runes/${data.stats.perk1}.png`,
      import.meta.url,
    ).href;
    for (let i = 0; i <= 6; i++) {
      this.itemIcons[i] = getItem(data.stats[`item${i}`] || 0);
    }
    this.KDA = `${data.stats.kills}/${data.stats.deaths}/${data.stats.assists}`;
    this.totalMinionsKilled = data.stats.totalMinionsKilled;
    this.totalGold = goldFormat(data.stats.goldEarned);
    this.totalDamageDealtToChampions = goldFormat(
      data.stats.totalDamageDealtToChampions,
    );
  }
}
const getItem = (i: number) => {
  if (i === 0) {
    return "";
  }
  return `https://game.gtimg.cn/images/lol/act/img/item/${i}.png`;
};
export const getspellImgUrl = (spellId: number) => {
  switch (spellId) {
    case 4:
      return "https://game.gtimg.cn/images/lol/act/img/spell/Summoner_flash.png";
    case 14:
      return "https://game.gtimg.cn/images/lol/act/img/spell/SummonerIgnite.png";
    case 11:
      return "https://game.gtimg.cn/images/lol/act/img/spell/Summoner_smite.png";
    case 6:
      return "https://game.gtimg.cn/images/lol/act/img/spell/Summoner_haste.png";
    case 12:
      return "https://game.gtimg.cn/images/lol/act/img/spell/Summoner_teleport.png";
    case 21:
      return "https://game.gtimg.cn/images/lol/act/img/spell/SummonerBarrier.png";
    case 3:
      return "https://game.gtimg.cn/images/lol/act/img/spell/Summoner_exhaust.png";
    case 1:
      return "https://game.gtimg.cn/images/lol/act/img/spell/Summoner_boost.png";
    case 7:
      return "https://game.gtimg.cn/images/lol/act/img/spell/Summoner_heal.png";
    case 32:
      return "https://game.gtimg.cn/images/lol/act/img/spell/Summoner_Mark.png";
  }
  return "https://game.gtimg.cn/images/lol/act/img/spell/SummonerMana.png";
};
export class GameDetailedInfo {
  gameCreation: string;
  gameModel: string;
  gameTime: string;
  team1: ITeam = {
    kill: 0,
    death: 0,
    assists: 0,
    goldEarned: 0,
    win: false,
  };
  team2: ITeam = {
    kill: 0,
    death: 0,
    assists: 0,
    goldEarned: 0,
    win: false,
  };
  participants: [];
  participantIdentities: [];
  teams1List: [] = [];
  teams2List: [] = [];
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  constructor(data: any) {
    this.gameCreation = formatDate(data.gameCreation);
    this.gameModel = querGameType(data.queueId);
    this.gameTime = Math.ceil(data.gameDuration / 60).toString();
    this.participants = data.participants;
    this.participantIdentities = data.participantIdentities;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    data.participants?.forEach((item: any, index: number) => {
      if (item.teamId === 100) {
        this.team1.kill = this.team1.kill + item.stats.kills;
        this.team1.death = this.team1.death + item.stats.deaths;
        this.team1.assists = this.team1.assists + item.stats.assists;
        this.team1.goldEarned = this.team1.goldEarned + item.stats.goldEarned;
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        this.teams1List.push({
          player: this.participantIdentities[index],
          champion: new champion(item),
        });
      } else {
        this.team2.kill = this.team2.kill + item.stats.kills;
        this.team2.death = this.team2.death + item.stats.deaths;
        this.team2.assists = this.team2.assists + item.stats.assists;
        this.team2.goldEarned = this.team2.goldEarned + item.stats.goldEarned;
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        this.teams2List.push({
          player: this.participantIdentities[index],
          champion: new champion(item),
        });
      }
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      data.teams.forEach((item: any) => {
        if (item.teamId === 100) {
          this.team1.win = item.win === "Win";
        } else {
          this.team2.win = item.win === "Win";
        }
      });
    });
  }
}
export const getMatchDetail = async (gameId: string) => {
  // const response = new GameDetailedInfo(
  //   await invoke("get_match_detail", { gameId: gameId }),
  // );
  const response = await invoke("get_match_detail", {
    gameId: gameId.toString(),
  });

  const gameDeatil = new GameDetailedInfo(response);
  return gameDeatil;
};
function formatDate(gameCreation: number): string {
  const date = new Date(gameCreation);
  const month = String(date.getMonth() + 1).padStart(2, "0"); // 获取月份并补零
  const day = String(date.getDate()).padStart(2, "0"); // 获取日期并补零
  const hour = String(date.getHours()).padStart(2, "0");
  const min = String(date.getMinutes()).padStart(2, "0");
  return `${month}-${day} ${hour}:${min}`;
}
